<?php

namespace app\stationpc\controller;

use app\model\StationOrderLog as ModelStationOrderLog;
use app\logic\sms\SMS as LogicSms;
use app\model\StationInfo as ModelStationInfo;
use app\model\Bankcard as ModelBankcard;
use app\model\WithdrawLog as ModelWithdrawLog;
use app\model\CashStream as ModelCashStream;
use app\model\SysSetting as ModelSysSetting;
use app\model\StationBoxOrderLog as ModelStationBoxOrderLog;


class AccountManagement extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 站点信息 \n
     * URI : /stationpc/site.information
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  stationId     | int    | 站点ID
     *  branchName    | string | 站点名称
     *  stationNumber | string | 站点编号
     *  legalName     | string | 法人姓名（真实姓名）
     *  mobile        | string | 手机号
     *  type          | int    | 站点类型
     *  address       | string | 站点地址
     *  level         | string | 站点等级
     *  balance       | string | 账户余额
     *  countBankcard | int    | 银行卡个数 1:私人承包；2：官方站点；0:其他
     *
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function siteInformation(){
        $arr_data = ModelStationInfo::getInstance()->toFind(['id' => $this->station_id], 'id AS stationId,branch_name AS branchName,station_number AS stationNumber,type,address,level,balance,legal_name AS legalName,mobile');
        $arr_data['countBankcard'] = (int)ModelBankcard::getInstance()->toCount(['map_id' => $this->station_id, 'type' => 3]);
        $arr_data['address'] = (string)$this->packingListDetailsAddress($arr_data['address']);
        _returnData($arr_data);
    }

    /**
     * 银行卡列表 \n
     * URI : /stationpc/site.information.bankcard.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  stationId     | int    | 站点ID
     *  branchName    | string | 站点名称
     *  stationNumber | string | 站点编号
     *  type          | int    | 站点类型
     *  address       | string | 站点地址
     *  level         | string | 站点等级
     *  balance       | string | 账户余额
     *  countBankcard | int    | 银行卡个数 1:私人承包；2：官方站点；0:其他
     *
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function siteInformationBankcardList(){
        $arr_list = ModelBankcard::getInstance()->toSelect(['map_id' => $this->station_id, 'type' => 3], 'id AS bankcardId,bank_name AS bankName,card_number AS cardNumber,gmt_create AS gmtCreate');
        _returnData(['list' => $arr_list]);
    }

    /**
     * 银行卡列表 - 删除 \n
     * URI : /stationpc/bankcard.list.delete
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  bankcardId        | int | 银行卡ID
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function siteInformationBankcardListDelete(){
        $int_bankcard_id = $this->_getContentParams('bankcardId');
        _toValidate('id', $int_bankcard_id, 10009);
        ModelBankcard::getInstance()->toDelete(['id' => $int_bankcard_id]);
        _returnData();
    }

    /**
     * 添加银行卡 \n
     * URI : /stationpc/bankcard.list.add
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  realName    | string | 必填 | 真实姓名
     *  cardNumber  | string | 必填 | 卡号
     *  mobile      | string | 必填 | 站点编号
     *  code        | string | 必填 | 验证码
     *  passKey     | string | 必填 | 短信key
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bankcardListAdd()
    {
        $str_real_name = $this->_getContentParams('realName');
        $str_card_number = $this->_getContentParams('cardNumber');
        $str_mobile = $this->_getContentParams('mobile');
        $str_code = $this->_getContentParams('code');
        $str_pass_key = $this->_getContentParams('passKey');

        _toCompare($str_real_name, '==', '', 15024);
        //获取快递员信息
        $field_info = "id,legal_name,state";
        $dataInfo = ModelStationInfo::getInstance()->toFind(['id' => $this->station_id], $field_info);
//        //判断用户是否已经实名认证
//        _toCompare($dataInfo['auth_state'], '==', '0', 15001);
//        //判断姓名
        _toCompare($dataInfo['legal_name'], '!=', $str_real_name, 15002); //该银行卡姓名与真实姓名不符
        //判断卡号
        _toCompare($str_card_number, '==', '', 15003); //请输入您的银行卡号
        if (!preg_match('/^\d*$/', $str_card_number)) {
            _returnError('15005');//银行卡号不合法
        }

        //判断卡号是否重复
        $cardList = ModelBankcard::getInstance()->toSelect(['state' => 1, 'type' => 3, 'map_id' => $this->station_id]);
        foreach ($cardList as $key => $value) {
            _toCompare($str_card_number, '==', $value['card_number'], 15006);
        }
        //验证验证码

        if (empty($str_code)) _returnError('19002');
        $res_code = LogicSms::getInstance()->verifySMS('bank_card', $str_mobile, $str_pass_key, $str_code, 'stationpc');
        _toCompare($res_code, '===', false, 11005); // 验证码不正确

        //获取用户身份证号
        $id_number = ModelStationInfo::getInstance()->toValue(['id' => $this->station_id], 'legal_idcard_no');
        //验证银行卡四要素
        $res_boll = ModelBankcard::getInstance()->aliYunVerifyBankCard4($str_real_name, $id_number, $str_card_number, $str_mobile);
        _toCompare($res_boll,'==',false,15028);

        //获取卡片信息
        $cardInfo = ModelBankcard::getInstance()->aliYunCheckBankCardType($str_card_number);
        //写入数据库
        $data['card_logo'] = $cardInfo['logo'];
        $data['bank_name'] = $cardInfo['bank'];
        $data['card_number'] = $str_card_number;
        $data['type'] = 3;
        $data['map_id'] = $this->station_id;
        $data['real_name'] = $dataInfo['legal_name'];
        $data['mobile'] = $str_mobile;
        $add_res = ModelBankcard::getInstance()->toAdd($data);
        if ($add_res) _returnData();
        _returnError(10010);
    }

    /**
     * 业务信息 - 出入库列表 \n
     * URI : /stationpc/for.the.information.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  gmtStart | string | 选填 | 开始时间（'2018-04-01 00:00:00'）
     *  gmtEnd   | string | 选填 | 结束时间（'2018-04-02 23:59:59'）
     *  order    | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  time        | string | 时间
     *  accessNum   | string | 入库量
     *  outboundNum | string | 出库量
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function forTheInformationList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $str_gmt_start = $this->_getContentParams('gmtStart'); //'2018-04-01 00:00:00';//
        $str_gmt_end = $this->_getContentParams('gmtEnd');
        $str_order = $this->_getContentParams('order');
        $arr_where = [
            'station_id' => $this->station_id,
        ];
        if($str_gmt_start != '' && $str_gmt_end != ''){
            _toCompare(($str_gmt_start > $str_gmt_end), '===', true, 21010);
            $arr_where['gmt_create'] = ["BETWEEN" ,[$str_gmt_start, $str_gmt_end]];
        }
        $int_total_rows = ModelStationOrderLog::getInstance()->toLeftJoinCount($arr_where);
        $str_order = $this->listOrder('gmt_create', $str_order);
        $order = $str_order.", substr(gmt_create,1,10)";

        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = "DATE_FORMAT(gmt_create,'%Y-%m-%d') as time,sum(case when state='40' then 1 else 0 end) AS accessNum,sum(case when state='60' then 1 else 0 end) AS outboundNum" ;
        $arr_data = ModelStationOrderLog::getInstance()->toLeftJoinSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $order);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_data]);
    }

    /**
     * 业务信息 - 拆装箱列表 \n
     * URI : /stationpc/dismounting.information.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  gmtStart | string | 选填 | 开始时间（'2018-04-01 00:00:00'）
     *  gmtEnd   | string | 选填 | 结束时间（'2018-04-02 23:59:59'）
     *  order    | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  time            | string | 时间
     *  packingNum      | string | 装箱量
     *  dismountingNum  | string | 拆箱量
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dismountingInformationList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $str_gmt_start = $this->_getContentParams('gmtStart'); //'2018-04-01 00:00:00';//
        $str_gmt_end = $this->_getContentParams('gmtEnd');
        $str_order = $this->_getContentParams('order');
        $arr_where = [
            'station_id' => $this->station_id,
        ];
        if($str_gmt_start != '' && $str_gmt_end != ''){
            _toCompare(($str_gmt_start > $str_gmt_end), '===', true, 21010);
            $arr_where['gmt_create'] = ["BETWEEN" ,[$str_gmt_start, $str_gmt_end]];
        }
        $int_total_rows = ModelStationBoxOrderLog::getInstance()->toLeftJoinCount($arr_where);
        $str_order = $this->listOrder('gmt_create', $str_order);
        $order = $str_order.", substr(gmt_create,1,10)";

        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = "DATE_FORMAT(gmt_create,'%Y-%m-%d') as time,sum(case when state='10' then 1 else 0 end) AS packingNum,sum(case when state='15' then 1 else 0 end) AS dismountingNum" ;
        $arr_data = ModelStationBoxOrderLog::getInstance()->toLeftJoinSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $order);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_data]);

    }

    /**
     * 提现记录列表 \n
     * URI : /stationpc/withdrawal.record.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  withdrawNumber    | string | 选填 | 订单号
     *  state             | string | 选填 | 提现状态0:申请成功；1：提现中；2：提现完成；3:提现失败
     *  gmtStart          | string | 选填 | 开始时间（'2018-04-01 00:00:00'）
     *  gmtEnd            | string | 选填 | 结束时间（'2018-04-02 23:59:59'）
     *  order             | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  withdrawLogId        | int    | 提现ID
     *  withdrawNumber       | string | 订单号
     *  state                | int    | 提现状态0:申请成功；1：提现中；2：提现完成；3:提现失败
     *  money                | string | 提现金额
     *  balance              | string | 余额
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function WithdrawalRecordList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $str_withdraw_number = $this->_getContentParams('withdrawNumber');     //订单号
        $str_state = $this->_getContentParams('state');     //提现状态0:申请成功；1：提现中；2：提现完成；3:提现失败'
        $str_gmt_start = $this->_getContentParams('gmtStart'); //'2018-04-01 00:00:00';//
        $str_gmt_end = $this->_getContentParams('gmtEnd');
        $str_order = $this->_getContentParams('order');

        $arr_where = $this->WithdrawalRecordListWhere($str_withdraw_number, $str_state, $str_gmt_start, $str_gmt_end);
        $str_order = $this->listOrder('gmt_create', $str_order);

        $int_total_rows = ModelWithdrawLog::getInstance()->toCount($arr_where);

        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS withdrawLogId,withdraw_number AS withdrawNumber,money,state,balance';
        $arr_list = ModelWithdrawLog::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $str_order);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_list]);
    }

    /**
     * 提现记录列表 -> 筛选
     * @param $str_withdraw_number
     * @param $str_state
     * @param $str_gmt_start
     * @param $str_gmt_end
     * @return array
     */
    private function WithdrawalRecordListWhere($str_withdraw_number, $str_state, $str_gmt_start, $str_gmt_end){
        $arr_where = [
            'type' => 3,
            'map_id' => $this->station_id,
        ];
        if($str_gmt_start != '' && $str_gmt_end != ''){
            _toCompare(($str_gmt_start > $str_gmt_end), '===', true, 21010);
            $arr_where['gmt_create'] = ["BETWEEN" ,[$str_gmt_start, $str_gmt_end]];
        }
        if($str_withdraw_number != ''){
            $arr_where['withdraw_number'] = ['like', "%$str_withdraw_number%"];
        }
        if($str_state != ''){
            $arr_where['state'] = $str_state;
        }
        return $arr_where;
    }

    /**
     * 提现记录列表-> 删除 \n
     * URI : /stationpc/withdrawal.record.list.delete
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  withdrawLogId     | int | 必填   | 提现ID
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function WithdrawalRecordListDelete(){
        $str_withdraw_log_id = $this->_getContentParams('withdrawLogId');     //提现ID
        _toValidate('id', $str_withdraw_log_id, 10009);
        ModelWithdrawLog::getInstance()->toDelete(['id' => $str_withdraw_log_id]);
        _returnData();
    }

    /**
     * 提现记录列表-> 详情 \n
     * URI : /stationpc/withdrawal.record.list.details
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  withdrawLogId     | int | 必填   | 提现ID
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     * withdrawLogId    | int    | 提现Id
     * withdrawNumber   | string | 订单号
     * money            | float  | 提现金额
     * bankName         | string | 银行名称
     * bankCardNumber   | string | 银行卡号
     * realName         | string | 账户名
     * gmtCreate        | string | 提现时间
     * state            | int    | 提现状态0:申请成功；1：提现中；2：提现完成；3:提现失败',
     * gmtModified      | string | 到账时间
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function WithdrawalRecordListDetails()
    {
        $str_withdraw_log_id = $this->_getContentParams('withdrawLogId');     //提现ID
        _toValidate('id', $str_withdraw_log_id, 10009);
        $arr_data = ModelWithdrawLog::getInstance()->toFind(['id' => $str_withdraw_log_id], 'id AS withdrawLogId,withdraw_number AS withdrawNumber,money,bank_name AS bankName,bank_card_number AS bankCardNumber,real_name AS realName,gmt_create AS gmtCreate,state,gmt_modified AS gmtModified');
        $arr_data['withdrawalWay'] = '银行卡';
        _returnData(['data' => $arr_data]);
    }

    /**
     * 收入明细列表 \n
     * URI : /stationpc/cash.stream.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  page     | int    | 必填 | 页码
     *  listRows | int    | 必填 | 一页的条数
     *
     *  orderNumber       | string | 选填 | 订单编号
     *  describe          | string | 选填 | 收入类型  中转入库 揽收入库
     *  gmtStart          | string | 选填 | 开始时间（'2018-04-01 00:00:00'）
     *  gmtEnd            | string | 选填 | 结束时间（'2018-04-02 23:59:59'）
     *  order             | string | 必填 | 必填倒序（order = ‘desc’）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  totalPages  | string | 总页数
     *  data        | array  | 列表
     *
     *  cashStreamId         | int    | 收入明细 （ID）
     *  mapId                | int    | 站点id
     *  orderNumber          | string | 订单编号
     *  describe             | int    | 收入类型  （中转入库） (揽收入库)
     *  money                | string | 收入金额
     *  gmtCreate            | string | 收入时间
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function CashStreamList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $str_order_number = $this->_getContentParams('orderNumber');     //订单编号
        $str_describe = $this->_getContentParams('describe');     //收入类型  中转入库 揽收入库
        $str_gmt_start = $this->_getContentParams('gmtStart'); //'2018-04-01 00:00:00';//
        $str_gmt_end = $this->_getContentParams('gmtEnd');
        $str_order = $this->_getContentParams('order');

        $arr_where = $this->CashStreamListWhere($str_order_number, $str_describe, $str_gmt_start, $str_gmt_end);
        $str_order = $this->listOrder('gmt_create', $str_order);

        $int_total_rows = ModelCashStream::getInstance()->toCount($arr_where);

        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'id AS cashStreamId,money,describe,gmt_create AS gmtCreate,order_number AS orderNumber,map_id AS mapId';
        $arr_list = ModelCashStream::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, $str_order);
        _returnData(['totalPages' => $int_total_pages, 'data' => $arr_list]);
    }

    /**
     * 收入明细列表 -> 筛选
     * @param $str_order_number
     * @param $str_describe
     * @param $str_gmt_start
     * @param $str_gmt_end
     * @return array
     */
    private function CashStreamListWhere($str_order_number, $str_describe, $str_gmt_start, $str_gmt_end){
        $arr_where = [
            'map_type' => 3,
            'map_id' => $this->station_id,
            'is_income' => 0,
        ];
        if($str_gmt_start != '' && $str_gmt_end != ''){
            _toCompare(($str_gmt_start > $str_gmt_end), '===', true, 21010);
            $arr_where['gmt_create'] = ["BETWEEN" ,[$str_gmt_start, $str_gmt_end]];
        }
        if($str_order_number != ''){
            $arr_where['order_number'] = ['like', "%$str_order_number%"];
        }
        if($str_describe != ''){
            $arr_where['describe'] = $str_describe;
        }
        return $arr_where;
    }

    /**
     * 收入明细列表-> 删除 \n
     * URI : /stationpc/cash.stream.list.delete
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  cashStreamId     | int | 必填   | 收入明细 （ID）
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function CashStreamListDelete(){
        $str_cash_stream_id = $this->_getContentParams('cashStreamId');     //收入明细 （ID）
        _toValidate('id', $str_cash_stream_id, 10009);
        ModelCashStream::getInstance()->toDelete(['id' => $str_cash_stream_id]);
        _returnData();
    }

    /**
     * 本站点的所有银行卡数据 \n
     * URI : /stationpc/station.bank.list
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  -------- | ------ | ----
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     * bankId       | int    |  收入明细 （ID）
     * cardNumber   | string |  卡号
     * balance      | float  |  可提现额度
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function stationBankList(){
        $arr_data = ModelBankcard::getInstance()->toSelect(['map_id' => $this->station_id, 'type' => 3], 'id AS bankId,card_number AS cardNumber');
        $str_balance = ModelStationInfo::getInstance()->toValue(['id' => $this->station_id], 'balance');
        _returnData(['data' => $arr_data, 'balance' => $str_balance]);
    }

    /**
     * 提现 \nbusinessInformationList
     * URI : /stationpc/station.withdraw.submit
     * @param :
     *  name     | type   | null| description
     * ----------|--------|-----|-------------
     *  bankCardId     | int | 必填   | 银行卡id
     *  bankCardNumber     | string | 必填   | 银行卡号
     *  money     | float | 必填   | 提现金额
     *  serverCharge     | float | 必填   | 手续费
     *
     *  @return :
     *  name        | type   | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function withdrawSubmit()
    {
        $bankCardId = $this->_getContentParams('bankCardId');//银行卡id

        $bankCardNumber = $this->_getContentParams('bankCardNumber'); //银行卡号

        $money = $this->_getContentParams('money');//提现金额

        _toValidate('money', $money, 11007);

        $serverCharge = $this->_getContentParams('serverCharge');//手续费

        _toValidate('money', $serverCharge, 11007);

//        $obj_pay_pwd = new \app\logic\PayPassword($this->str_module_name);
        // 检查支付密码是否正确

//        $bool_is_right = $obj_pay_pwd->checkPayPasswordIsRight($this->station_id, $this->_getContentParams('payPassword'));
//        _toCompare($bool_is_right, '!=', true, 10016);

        //获取系统手续费，与前台传入的手续费做比对
        $sysServerCharge = ModelSysSetting::getInstance()->getProcedureFee($money);

        _toCompare($serverCharge, '!=', $sysServerCharge, 11008); // 提现金额加上手续费不能大于账户余额

        $arr_user_info = ModelStationInfo::getInstance()->toFind(['id' => $this->station_id], 'balance,legal_name as realName');
        _toCompare($money, '>', $arr_user_info['balance'] - $sysServerCharge, 11009);

        // 查找是否存在银行卡
        $bankCardInfo = ModelBankCard::getInstance()->toFind(['map_id' => $this->station_id, 'type' => 3, 'card_number' => $bankCardNumber]);
        _toCompare(empty($bankCardInfo), '===', true, 15028);

        //相关id
        $insertId = ModelWithdrawLog::getInstance()->addWithdrawLog($this->station_id,$bankCardId,$money,3,$bankCardNumber,$bankCardInfo['bank_name'],$arr_user_info['realName'],$serverCharge);
        if (!$insertId) _returnError(10011);
        //相关订单号
        $orderNumber = ModelWithdrawLog::getInstance()->toValue(['id'=>$insertId],'withdraw_number');
        //代付接口随机订单号
        $str_out_trade_no = $this->_getRandomString(30);

        //扣除提现金额，并写入流水
        $subtractMoney = ModelCashStream::getInstance()->editBalance(3,$this->station_id,1,$money,'提现',$orderNumber,$insertId);
        //扣除手续费，并写入流水
        $subtractServerCharge = ModelCashStream::getInstance()->editBalance(3,$this->station_id,1,$serverCharge,'提现手续费',$orderNumber,$insertId);

        if ($subtractMoney && $subtractServerCharge) {
            // 调用支付通道
            $obj = new \app\logic\XtPay(config('xtpay.mer_id'), config('xtpay.secret_key'));
            $bool_is_success = true;// $obj->payForBank($money, $str_out_trade_no, $arr_user_info['realName'], $bankCardInfo['card_number'], $bankCardInfo['bank_name']);
            if ($bool_is_success === true) {
                // 更新订单状态
                ModelWithdrawLog::getInstance()->toUpdate(['id' => $insertId], ['state' => 2]);
                _returnData(['recordId' => $insertId]);
            } else {
                //退回提现金额，写入余额记录表
                ModelCashStream::getInstance()->editBalance(3,$this->station_id,0,$money,'提现金额返还',$orderNumber,$insertId);//扣除提现金额，并写入流水
                ModelCashStream::getInstance()->editBalance(3,$this->station_id,0,$serverCharge,'提现手续费返还',$orderNumber,$insertId);//扣除手续费，并写入流水
                // 更新订单状态
                ModelWithdrawLog::getInstance()->toUpdate(['id' => $insertId], ['state' => 3]);
            }
        }
        _returnError(11011); // 提现失败请重试

    }

}
